package xcmg.device.service.warehouse;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.warehouse.WarehouseInventoryHistory;
import xcmg.device.dao.mapper.warehouse.WarehouseInventoryHistoryMapper;
import xcmg.device.service.vo.warehouse.WarehouseInventoryHistoryReportVO;
import xcmg.device.service.vo.warehouse.WarehouseInventoryTurnOverResultVO;
import yb.ecp.fast.infra.infra.PageCommonVO;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class WarehouseInventoryHistoryService {
    @Autowired
    private WarehouseInventoryHistoryMapper warehouseInventoryHistoryMapper;

    @Transactional
    public void generateWarehouseInventoryHistory(Date createDate , String historyTime, String orgId){
        //获取需要保存的数据
        List<WarehouseInventoryHistory> warehouseInventoryHistories = warehouseInventoryHistoryMapper.selectWarehouseStock(orgId);
        for(WarehouseInventoryHistory history : warehouseInventoryHistories){
            history.setCreateDate(createDate);
            history.setIsDel(0);
            history.setHistoryTime(historyTime);
        }
        //先软删除掉orgId+historyTime已经存在记录
        warehouseInventoryHistoryMapper.deleteByOrgAndHistoryTime(orgId,historyTime);
        //批量保存数据
        warehouseInventoryHistoryMapper.batchInsert(warehouseInventoryHistories);
    }

    public List<WarehouseInventoryHistoryReportVO> warehouseInventoryHistoryReport(String orgId,String beginMonth){
        List<WarehouseInventoryHistoryReportVO> warehouseInventoryHistoryReports = warehouseInventoryHistoryMapper.warehouseInventoryHistoryReport(orgId,beginMonth);
        return warehouseInventoryHistoryReports;
    }

    public PageCommonVO<WarehouseInventoryTurnOverResultVO> turnoverReport(String orgId,Integer pageSize, Integer pageNum, Date outStartDate, Date outEndDate,
                                                                           String inventoryStartDate, String inventoryEndDate,
                                                                           String materialsNo,String materialsDesc){
        PageHelperPlus.startPage(pageNum, pageSize);
        List<WarehouseInventoryTurnOverResultVO> warehouseInventoryTurnOverResultVOS = warehouseInventoryHistoryMapper.turnOverList(orgId, outStartDate, outEndDate, inventoryStartDate, inventoryEndDate, materialsNo, materialsDesc);
        PageCommonVO result = new PageCommonVO();
        result.setPageInfo(new PageInfo(warehouseInventoryTurnOverResultVOS));
        result.setPageInfoList(warehouseInventoryTurnOverResultVOS);
        return result;
    }

    public List<WarehouseInventoryTurnOverResultVO> turnoverExport(String orgId, Date outStartDate, Date outEndDate,
                                                                           String inventoryStartDate, String inventoryEndDate,
                                                                           String materialsNo,String materialsDesc){
        List<WarehouseInventoryTurnOverResultVO> warehouseInventoryTurnOverResultVOS = warehouseInventoryHistoryMapper.turnOverList(orgId, outStartDate, outEndDate, inventoryStartDate, inventoryEndDate, materialsNo, materialsDesc);
        return warehouseInventoryTurnOverResultVOS;
    }

}
